package com.cksys.achievement.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cksys.achievement.entity.Result;
import com.cksys.achievement.entity.ResultPicture;
import com.cksys.achievement.entity.User;
import com.cksys.achievement.service.IResultPictureService;
import com.cksys.achievement.service.IResultService;
import com.cksys.achievement.service.IUserService;
import com.cksys.achievement.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tadechen
 * @since 2019-09-26
 */
@RestController
@RequestMapping("/resultPicture")
public class ResultPictureController {

    @Autowired
    IResultPictureService resultPictureService;

    @Autowired
    IUserService userService;

    @Autowired
    IResultService resultService;

    @PostMapping("/delete/{id}")
    public RestResponse deleteById(@PathVariable Integer id,
                                   @AuthenticationPrincipal UserDetails userDetails) {
        User user = userService.getByUserName(userDetails.getUsername());
        if (user == null) {
            throw new RuntimeException("用户错误!");
        }

        ResultPicture resultPicture = resultPictureService.getById(id);
        Result byId = resultService.getById(resultPicture.getRId());
        if (null == byId) {
            return RestResponse.fail("删除失败！");
        }
        if (!byId.getUId().equals(user.getId())) {
            return RestResponse.fail("您没有权限修改！");
        }

        boolean b = resultPictureService.removeById(id);
        if (b) {
            return RestResponse.ok(resultPicture);
        } else {
            return RestResponse.fail("删除失败！");
        }

    }

    @PostMapping("/upload/info")
    public RestResponse uploadInfo(ResultPicture resultPicture,
                                   @AuthenticationPrincipal UserDetails userDetails) {
        User user = userService.getByUserName(userDetails.getUsername());
        if (user == null) {
            throw new RuntimeException("用户错误!");
        }

        ResultPicture resultPicture1 = resultPictureService.getById(resultPicture.getId());
        Result byId = resultService.getById(resultPicture1.getRId());
        if (null == byId) {
            return RestResponse.fail("更新失败！");
        }
        if (!byId.getUId().equals(user.getId())) {
            return RestResponse.fail("您没有权限修改！");
        }

        resultPicture.setPath(null).setRId(null);
        boolean b = resultPictureService.updateById(resultPicture);
        if (b) {
            return RestResponse.ok(resultPicture);
        } else {
            return RestResponse.fail("更新失败！");
        }
    }

}
